package com.struts2.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Restrictions;

import com.struts2.model.Category;
import com.struts2.model.Color;
import com.struts2.model.Designer;
import com.struts2.model.Gender;
import com.struts2.model.Product_line;
import com.struts2.model.Product_item;
import com.struts2.model.Season;
import com.struts2.model.Size;
import com.struts2.model.Type;

public class InsertData {

	private int id;

	public int getId() {
		return id;
	}




	public void setId(int id) {
		this.id = id;
	}




	public String execute() throws ParseException {

		
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Product_item.class);
		criteria.add(Restrictions.eq("id", id));
		
		Product_item product_item = (Product_item) criteria.list().get(0);

		
	//	product_item.setApproval(true);
		
		session.save(product_item);
		/*
		 // Insert Type and Cat
 
		// T-Shirts/Tanks
		Type type1 = new Type("Crop Tops");
		Type type2 = new Type("Short sleeve");
		Type type3 = new Type("Long sleeve");
		Type type4 = new Type("Sleeveless");
		
		// Sweaters
		Type type5 = new Type("Cardigans");
		Type type6 = new Type("Sweaters");
		Type type7 = new Type("Pullovers");
		
		// Jackets
		Type type8 = new Type("Jackets");
		Type type9 = new Type("Coats");
		
		// Dresses
		Type type10 = new Type("Mini");
		Type type11 = new Type("Mid-length");
		Type type12 = new Type("Maxi");
		
		// Skirts 
		Type type15 = new Type("Long");
		
		// Pants
		Type type16 = new Type("Jeans");
		Type type17 = new Type("High waisted");
		Type type18 = new Type("Low rise");
		Type type19 = new Type("Jumpsuits");
		Type type20 = new Type("Sweatpants");
		Type type21 = new Type("Chinos");
		Type type22 = new Type("suit");

		
		//  girls
		Type type25 = new Type("Baby girls");
		Type type26 = new Type("Girls");
		
		// boys
		Type type27 = new Type("Baby boys");
		Type type28 = new Type("Boys");
		
		
		// Other
		Type type29 = new Type("Swimwear");
		Type type30 = new Type("Sportswear");
		Type type31 = new Type("Nightwear");
		Type type32 = new Type("Underwear");
		
		// Accessories
		Type type33 = new Type("Headwear");
		Type type34 = new Type("Eyewear");
		Type type35 = new Type("Bags");
		Type type36 = new Type("Socks");
		Type type37 = new Type("Leather");
		
		
		// cat
		Category cat1 = new Category("T-Shirts/Tanks");
		Category cat2 = new Category("T-Shirts");
		Category cat3 = new Category("Shirts");
		Category cat4 = new Category("Sweaters");
		Category cat5 = new Category("Jackets");
		Category cat6 = new Category("Dresses");
		Category cat7 = new Category("Skirts");
		Category cat8 = new Category("Pants");
		Category cat9 = new Category("Shorts");
		Category cat10 = new Category("Leggins");
		Category cat11 = new Category("Girls");
		Category cat12 = new Category("Boys");
		Category cat13 = new Category("Others");
		Category cat14 = new Category("Accessories");
		Category cat15 = new Category("Tank Tops");

		// set
		(cat1.getType()).add(type1);
		(cat1.getType()).add(type2);
		(cat1.getType()).add(type3);
		(cat1.getType()).add(type4);
		
		(cat2.getType()).add(type2);
		(cat2.getType()).add(type3);

		(cat3.getType()).add(type1);
		(cat3.getType()).add(type2);
		(cat3.getType()).add(type3);
		(cat3.getType()).add(type4);

		(cat4.getType()).add(type5);
		(cat4.getType()).add(type6);
		(cat4.getType()).add(type7);

		(cat5.getType()).add(type8);
		(cat5.getType()).add(type9);

		(cat6.getType()).add(type10);
		(cat6.getType()).add(type11);
		(cat6.getType()).add(type12);

		(cat7.getType()).add(type10);
		(cat7.getType()).add(type11);
		(cat7.getType()).add(type15);		
		
		(cat8.getType()).add(type16);
		(cat8.getType()).add(type17);
		(cat8.getType()).add(type18);
		(cat8.getType()).add(type19);
		(cat8.getType()).add(type20);
		(cat8.getType()).add(type21);
		(cat8.getType()).add(type22);
		
		(cat11.getType()).add(type25);
		(cat11.getType()).add(type26);


		(cat12.getType()).add(type27);
		(cat12.getType()).add(type28);
		
		(cat13.getType()).add(type29);
		(cat13.getType()).add(type30);
		(cat13.getType()).add(type31);
		(cat13.getType()).add(type32);
		
		(cat14.getType()).add(type33);
		(cat14.getType()).add(type34);
		(cat14.getType()).add(type35);
		(cat14.getType()).add(type36);
		(cat14.getType()).add(type37);


		session.save(cat1);
		session.save(cat3);
		session.save(cat4);
		session.save(cat5);
		session.save(cat6);
		session.save(cat7);
		session.save(cat8);
		session.save(cat9);
		session.save(cat10);
		session.save(cat11);
		session.save(cat12);
		session.save(cat13);
		session.save(cat14);
		session.save(cat15);
	
		 // Insert Type and Cat END

		
		
		
		
		
		 // COLOR add
		Color color1 = new Color("Red");
		Color color2 = new Color("Yellow");
		Color color3 = new Color("Orange");
		Color color4 = new Color("Green");
		Color color5 = new Color("Black");
		Color color6 = new Color("White");
		Color color7 = new Color("Blue");
		Color color8 = new Color("Purple");
		Color color9 = new Color("Brown");

		session.save(color1);
		session.save(color2);
		session.save(color3);
		session.save(color4);
		session.save(color5);
		session.save(color6);
		session.save(color7);
		session.save(color8);
		session.save(color9);
		
		 // COLOR END
		
		
		
		
		
		
		// SEASON add
		Season season1  = new Season("Winter");
		Season season2  = new Season("Spring");
		Season season3  = new Season("Summer");
		Season season4  = new Season("Autumn");

		session.save(season1);
		session.save(season2);
		session.save(season3);
		session.save(season4);

		// SEASON END

		// SIZE add
		
		Size size1  = new Size("XS");
		Size size2  = new Size("S");
		Size size3  = new Size("M");
		Size size4  = new Size("L");
		Size size5  = new Size("XL");
		Size size6  = new Size("XXL");
		session.save(size1);
		session.save(size2);
		session.save(size3);
		session.save(size4);
		session.save(size5);
		session.save(size6);

		
		// SIZE END

		// MATERIAL add
		
		Material material1 = new Material("furs");
		Material material2 = new Material("leather");
		Material material3 = new Material("cotton");
		Material material4 = new Material("polyester");
		Material material5 = new Material("Nylon");
		Material material6 = new Material("Spandex");
		Material material7 = new Material("hemp");
		Material material8 = new Material("rubber");
		Material material9 = new Material("jute");
		Material material10 = new Material("Duct tape");
		Material material11 = new Material("recycled paper");

		session.save(material1);
		session.save(material2);
		session.save(material3);
		session.save(material4);
		session.save(material5);
		session.save(material6);
		session.save(material7);
		session.save(material8);
		session.save(material9);
		session.save(material10);
		session.save(material11);
		
		
		
	
		// MATERIAL END
		
		
		
		 // GENDER add

		Gender gender1  = new Gender("Woman");
		Gender gender2  = new Gender("Man");
		Gender gender3  = new Gender("Kid");
		
		
		
		(gender1.getCategory()).add(cat1);
		(gender1.getCategory()).add(cat3);
		(gender1.getCategory()).add(cat4);
		(gender1.getCategory()).add(cat5);
		(gender1.getCategory()).add(cat6);
		(gender1.getCategory()).add(cat7);
		(gender1.getCategory()).add(cat8);
		(gender1.getCategory()).add(cat9);
		(gender1.getCategory()).add(cat10);
		(gender1.getCategory()).add(cat13);
		
		(gender2.getCategory()).add(cat2);
		(gender2.getCategory()).add(cat3);
		(gender2.getCategory()).add(cat4);
		(gender2.getCategory()).add(cat5);
		(gender2.getCategory()).add(cat8);
		(gender2.getCategory()).add(cat9);
		(gender2.getCategory()).add(cat13);
		(gender2.getCategory()).add(cat15);

		(gender3.getCategory()).add(cat11);
		(gender3.getCategory()).add(cat12);


		
		
		
		
		session.save(gender1);
		session.save(gender2);
		session.save(gender3);
		 // GENDER END
		*/
		
		
		/*// Insert Designer
		long l = 412343213124L;
		SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy");
		String dateInString = "31-08-1982";
		Date dob = sdf.parse(dateInString);
		Designer designer = new Designer("designerName", "password", "address",
				"M", "email", 123,
				"education", l, "username",
				"background", dob, true); 
		
		session.save(designer);
		*/ // Insert Designer END
		
		/*
		for(int i =24 ; i<39;i++){
		// Insert Product
		
			int designerId = 1;
			Product  product = createProduct("Product Name "+i, "Product Description "+i,designerId);
			
			 // Insert Product END
			
	
		
		// add product item (use id)
		
		int productId = i;
		int price = 5;
		int quantity = 6;
		int categoryId = 1;
		int colorId = 1;
		int genderId = 1;
		int materialId = 3;
		int seasonId = 2;
		int sizeId = 2;
		int typeId = 1;
		Product_item product_item = createProductItem(price, quantity, categoryId, colorId, genderId, materialId, productId,seasonId,sizeId, typeId);
		
		
		 // add product item (use id) END
		}
		*/
		
		
		/*	// add product item (use string)
			int productId = 1;
			int price = 5;
			int quantity = 6;
			Product_item product_item = createProductItem(price, quantity, "T-Shirts/Tanks", "Red", "Woman", "leather", productId, "Winter", "XS", "Sleeveless");
			session.save(product_item);
		
		*/ // add product item (use string) END
		
		session.getTransaction().commit();
		session.close();
		sF.close();
		
		
		System.out.print("" + product_item.getApproval());
		return "1";
	}

	
	
	
	public void /*Product_line*/ createProduct( String description, String name,
			int designerId) {
		
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();
		
		Date date = new Date();
		date.getTime();
		
		Criteria criteria1 = session.createCriteria(Designer.class);
		criteria1.add(Restrictions.eq("id", designerId));
		Designer designer= (Designer)criteria1.list().get(0);
		
//		Product_line product = new Product_line(date, description, name , designer);

//		session.save(product);

		session.getTransaction().commit();
		session.close();
		sF.close();
	//	return product;
	}
	
	public Product_item createProductItem(int price, int quantity,
			String categoryName, String colorName, String genderName,
			String materialName, int productId, String seasonName,
			String sizeName, String typeName) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria8 = session.createCriteria(Gender.class);
		criteria8.add(Restrictions.eq("name", genderName));
		Gender gender = (Gender) criteria8.list().get(0);

		Criteria criteria1 = session.createCriteria(Type.class);
		criteria1.add(Restrictions.eq("name", typeName));
		Type type = (Type) criteria1.list().get(0);

		Criteria criteria2 = session.createCriteria(Category.class);
		criteria2.add(Restrictions.eq("name", categoryName));
		Category category = (Category) criteria2.list().get(0);

		Criteria criteria3 = session.createCriteria(Color.class);
		criteria3.add(Restrictions.eq("name",colorName));
		Color color = (Color) criteria3.list().get(0);
		
	
		
		Criteria criteria5 = session.createCriteria(Product_line.class);
		criteria5.add(Restrictions.eq("id",productId ));
		Product_line product= (Product_line)criteria5.list().get(0);
		
		Criteria criteria6 = session.createCriteria(Season.class);
		criteria6.add(Restrictions.eq("name",seasonName ));
		Season season= (Season)criteria6.list().get(0);
		
		Criteria criteria7 = session.createCriteria(Size.class);
		criteria7.add(Restrictions.eq("name", sizeName));
		Size size= (Size)criteria7.list().get(0);
		
		
		
		Product_item product_item = new Product_item(product, type, category,
				size, color, quantity);
		
		session.getTransaction().commit();
		session.close();
		sF.close();
		return product_item ;
	}

	
	public Product_item createProductItem(int price, int quantity,
			int categoryId, int colorId, int genderId,
			int materialId, int productId, int seasonId,
			int sizeId, int typeId) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria8 = session.createCriteria(Gender.class);
		criteria8.add(Restrictions.eq("id", genderId));
		Gender gender = (Gender) criteria8.list().get(0);

		Criteria criteria1 = session.createCriteria(Type.class);
		criteria1.add(Restrictions.eq("id", typeId));
		Type type = (Type) criteria1.list().get(0);

		Criteria criteria2 = session.createCriteria(Category.class);
		criteria2.add(Restrictions.eq("id", categoryId));
		Category category = (Category) criteria2.list().get(0);

		Criteria criteria3 = session.createCriteria(Color.class);
		criteria3.add(Restrictions.eq("id",colorId));
		Color color = (Color) criteria3.list().get(0);
		
		
		Criteria criteria5 = session.createCriteria(Product_line.class);
		criteria5.add(Restrictions.eq("id",productId ));
		Product_line product= (Product_line)criteria5.list().get(0);
		
		Criteria criteria6 = session.createCriteria(Season.class);
		criteria6.add(Restrictions.eq("id",seasonId ));
		Season season= (Season)criteria6.list().get(0);
		
		Criteria criteria7 = session.createCriteria(Size.class);
		criteria7.add(Restrictions.eq("id", sizeId));
		Size size= (Size)criteria7.list().get(0);
		
		
		
		Product_item product_item = new Product_item(product, type, category,
				size, color, quantity);
		
		
		session.save(product_item);

		session.getTransaction().commit();
		session.close();
		sF.close();
		return product_item ;
	}




		
	
}
